﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    class Jogo5
    {
        Texture2D Tiro;
        Texture2D In;
        List<Persona> Tiros;
        List<Persona> Inimigos;
        //Persona[] Inimigos;
        Persona Voce;

        public Jogo5(Texture2D a, Texture2D j, Texture2D k)
        {
            Tiro = k;
            In = a;
            Tiros = new List<Persona>();
            Inimigos = new List<Persona>();
            Random _r = new Random();
            for (int b = 0; b < 10; b++)
            {
                Inimigos.Add(new Persona(_r.Next(900) + 100, _r.Next(400) + 100, a));
            }
            Voce = new Persona(400, 600, j);
        }

        public void Reseta()
        {
            Tiros.Clear();
            Inimigos.Clear();
            Random _r = new Random();
            for (int b = 0; b < 10; b++)
            {
                Inimigos.Add(new Persona(_r.Next(900) + 100, _r.Next(400) + 100, In));
            }
        }

        public char Update(KeyboardState ks, Tempo Time, Song[] Musicas)
        {
            if (Time.tempoRestante <= TimeSpan.FromSeconds(0))
            {
                MediaPlayer.Play(Musicas[2]);
                Time.tempoRestante = TimeSpan.FromSeconds(5);
                return '0';
            }
            if ((ks.IsKeyDown(Keys.Left) || (ks.IsKeyDown(Keys.A))) && Voce.Pos.X > 5)
            {
                if (ks.IsKeyDown(Keys.RightShift))
                {
                    Voce.Pos.X -= 5;
                }
                else
                {
                    Voce.Pos.X -= 7;
                }
            }
            if ((ks.IsKeyDown(Keys.Right) || (ks.IsKeyDown(Keys.D))) && Voce.Pos.X < 1000)
            {
                if (ks.IsKeyDown(Keys.RightShift))
                {
                    Voce.Pos.X += 5;
                }
                else
                {
                    Voce.Pos.X += 7;
                }
            }
            if (ks.IsKeyDown(Keys.Space) && Tiros.Count < 10 )
            {
                CriaTiro();
            }
            for (int a = 0; a < Tiros.Count;a++)
            {
                Tiros[a].Pos.Y -= 9;
                if (Tiros[a].Pos.Y <= 0)
                {
                    Tiros.RemoveRange(0, 1);
                }
            }
            if (Colide(Time))
            {
                //MediaPlayer.Play(Musicas[3]);
                Time.ResetaTempo();
                return '6';
            }
            return '5';
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Voce.Img, Voce.Pos, Color.White);
            foreach (Persona a in Inimigos)
            {
                spriteBatch.Draw(a.Img, a.Pos, Color.White);
            }
            foreach (Persona a in Tiros)
            {
                spriteBatch.Draw(a.Img, a.Pos, Color.White);
            }
        }

        public void CriaTiro()
        {
            Tiros.Add(new Persona((int)Voce.Pos.X+15,(int)Voce.Pos.Y-20,Tiro));
        }

        public bool Colide(Tempo Time)
        {
            for (int a = 0; a < Tiros.Count; a++)
            {
                Rectangle r1 = new Rectangle((int)Tiros[a].Pos.X, (int)Tiros[a].Pos.Y, (int)Tiros[a].Img.Width, (int)Tiros[a].Img.Height);
                for (int b = 0; b < Inimigos.Count; b++)
                {
                    Rectangle r2 = new Rectangle((int)Inimigos[b].Pos.X, (int)Inimigos[b].Pos.Y, (int)Inimigos[b].Img.Width, (int)Inimigos[b].Img.Height);
                    if (r1.Intersects(r2))
                    {
                        Tiros.Remove(Tiros[a]);
                        Inimigos.Remove(Inimigos[b]);
                        Time.Ptos += 100;
                        return Verifica();
                    }
                }  
            }
            return false;
        }

        public bool Verifica()
        {
            if (Inimigos.Count == 0)
            {
                return true;
            }
            return false;
        }
    }
}
